<?php
/**
 * CreateTime: 2023/9/4 17:21
 */

namespace App\Console\Commands;

use App\Func\HuaWei\ObsFunc;
use App\Models\MeLawCase\MeLawCaseBindMeDetails;
use App\Models\MeLawCase\MeWhCacheModel;
use App\Tools\USails\USailsBasic;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;

class MeWhCacheCommand  extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cacheWhPhone';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command cacheWhPhone';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $whCacheModel = new MeWhCacheModel();
        $detailBindModel = new MeLawCaseBindMeDetails();
        $us = new USailsBasic();
        $obsFunc = new ObsFunc();
        while(true){
            $arr = $detailBindModel->select(['me_details_uuid'])->where('can_cache',2)->groupBy('me_details_uuid')->get();
            foreach($arr as $value){
                // 获取联通缓存
                $us_req = $us->getMeCdrsDetailsByUUidNotFmt($value['me_details_uuid']);
                if(isset($us_req) && $us_req->code == 2000){
                    echo "进入obs处理";
                    // 处理OBS
                    $fileUrl = $us_req->data->recordUrl;
                    $contents = @file_get_contents($fileUrl);
                    if($contents === false){
                        continue;
                    }
                    $tempFile = tempnam(sys_get_temp_dir(), 'file');
                    file_put_contents($tempFile, $contents);
                    $res = Storage::disk('obs')->putFile("/", $tempFile);
                    // 存储fileModel
                    $obsInfo = $obsFunc->uploadVideoByPath(Storage::path("obs/".$res));
                    $obsUrl = "";
                    if($obsInfo['flag']){
                        $obsUrl = $obsInfo['url'];
                        // 删除本地存储的文件
                        unlink(Storage::path("obs/".$res));
                    }

                    $whCacheModel->add([
                        'answer_time' => $us_req->data->answerTime,
                        'create_time' => $us_req->data->createTime,
                        'talkDuration' => $us_req->data->talkDuration,
                        'callDuration' => $us_req->data->callDuration,
                        'caller' => $us_req->data->caller,
                        'callee' => $us_req->data->callee,
                        'outPhone' => $us_req->data->outPhone,
                        'uuid' => $value['me_details_uuid'],
                        'obs_url' => $obsUrl,
                    ]);
                }

                // 替换同一个uuid的为已发函
                $detailBindModel->where('me_details_uuid',$value['me_details_uuid'])->update([
                    'can_cache' => 1
                ]);
            }
            die();
            sleep(1);

        }
    }
}
